package com.pm.controller.negative;

import com.pm.common.Response;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.negative.NegativeSource;
import com.pm.service.negative.NegativeSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "负面来源")
@UserLoginToken
@RestController
@RequestMapping("/negativeSource")
public class NegativeSourceController {
    @Autowired
    NegativeSourceService negativeSourceService;

    @ApiOperation("查询负面来源")
    @PostMapping("/getAllSource")
    @ResponseBody
    public Response<List<NegativeSource>> getAll(@RequestParam("id") Integer id) {
        List<NegativeSource> departments = negativeSourceService.selectAllDepartment(id);
        Response<List<NegativeSource>> response = new Response<>(ExpCodeState.query_data_success, departments);
        return response;
    }

    @ApiOperation("删除负面来源")
    @PostMapping("/DeleteSource")
    @ResponseBody
    @MyLog(value = "删除负面来源")
    public Response<Integer> deleteSource(@RequestParam("id") Integer id) {
        boolean res = negativeSourceService.deleteSource(id);
        if (res) {
            return new Response<>(ExpCodeState.remove_data_success);
        } else {
            return new Response<>(ExpCodeState.remove_data_failed);
        }
    }

    @ApiOperation("修改负面来源")
    @PostMapping("/updateSource")
    @ResponseBody
    @MyLog(value = "修改负面来源")
    public Response<Integer> updateChecklist(@RequestBody NegativeSource negativeSource) {
        boolean res = negativeSourceService.updateSource(negativeSource);
        if (res) {
            return new Response<>(ExpCodeState.update_data_success);
        } else {
            return new Response<>(ExpCodeState.update_data_failed);
        }
    }


    /**
     * 添加负面清单
     * @return
     */
    @ApiOperation("添加负面来源")
    @PostMapping("/addSource")
    @ResponseBody
    @MyLog(value = "新增负面来源")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> addSource(@RequestBody NegativeSource negativeSource) {
        Response<Integer> response = null;
        Integer result = negativeSourceService.addSource(negativeSource);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }


    /**
     * 根据id查部门
     * @return
     */
    @ApiOperation("根据Id查询来源")
    @PostMapping("/getSourceId")
    @ResponseBody
    public Response<NegativeSource> getSourceId(Integer id) {
        NegativeSource negativeSource = negativeSourceService.getSourceId(id);
        Response<NegativeSource> response = new Response<>(ExpCodeState.query_data_success, negativeSource);
        return response;
    }

}
